import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
  InternalServerErrorException,
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class ExceptionInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      catchError((err) => {
        // 在这里处理你的错误，例如日志记录、错误格式化等
        // 然后返回一个合适的响应给客户端
        // 这里只是一个示例，具体实现根据你的需求来
        console.error('Error during request: ', err.response);
        return throwError(() => new InternalServerErrorException(err.response));
      }),
    );
  }
}
